//
//  HRUserCenterViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-8.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRUserCenterViewController.h"
#import "HRMyPreferentViewController.h"
#import "HRMyCollectViewController.h"
#import "HRTakeAddressViewController.h"
#import "HRUserGoodsListViewController.h"
#import "HRMessageViewController.h"
#import "HRAppDelegate.h"
#import "HRUser.h"
#import "HRUserScoresViewController.h"

#import "HROrderApplyListViewController.h"

#define ORDER_BTN_TAG 1000

@interface HRUserCenterViewController (){
    NSMutableArray *arrayTable;
}

@end

@implementation HRUserCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
            }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    

    if (__CUR_IOS_VERSION >= __IPHONE_7_0)
    {
        self.edgesForExtendedLayout= UIRectEdgeNone;
    }
    
    arrayTable = [[NSMutableArray alloc] initWithObjects:@"所有订单",@"我的优惠券",@"收货管理地址",@"我的收藏", nil];
    [_tableView reloadData];
    
    //刷新页面数据
    [self refreshViewData];
    
    //去除多余的间隔线
    UIView *footerView=[[UIView alloc] init];
    [footerView setBackgroundColor:[UIColor clearColor]];
    
    [_tableView setTableFooterView:footerView];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    HRUser *userInfo=[HRCommonData shareCommonData].curUser;
    
    if (userInfo) {
        [_loginButton setTitle:userInfo.userName forState:UIControlStateNormal];
        
        if ([HRCommonData shareCommonData].isUserScoreChanged) {
            [self loadUserScore];
        }else{
            _lblScore.hidden = NO;
            _lblScore.text = [NSString stringWithFormat:@"海尔积分(%@)",userInfo.userScore ];
        }
    }else{
        [_loginButton setTitle:@"登录/注册" forState:UIControlStateNormal];
        _lblScore.hidden = YES;
    }
    
    [super viewWillAppear:animated];
}

-(void)refreshViewData{
    //_numView.curNum = 10;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods

//打开配置界面
- (IBAction)onTapOpenSetup:(id)sender {
    
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:STORYBOARDNAME_SETUP bundle:nil];
    
    [self.tabBarController.navigationController pushViewController:storyboard.instantiateInitialViewController animated:YES];
    
}

//打开用户登录页面
- (IBAction)onTapOpenLogin:(id)sender {
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            //用户登录以后
        }];

    }else{ //进入积分详细列表
        HRUserScoresViewController *viewC = [[HRUserScoresViewController alloc] initWithNibName:@"HRUserScoresViewController" bundle:nil];
        [self.tabBarController.navigationController pushViewController:viewC animated:YES];
    }
}



#pragma mark - TableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayTable.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48.5;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"userCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    for (UIView *v in cell.contentView.subviews) {
        [v removeFromSuperview];
    }
    
    UILabel *lblT = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 26.5)];
    lblT.backgroundColor = [UIColor clearColor];
    lblT.font = [UIFont systemFontOfSize:15];
    lblT.text = [arrayTable objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lblT];
    
    UIView *sepLine = [[UIView alloc] initWithFrame:CGRectMake(0, 47.5, self.view.bounds.size.width, 1)];
    sepLine.backgroundColor = [UIColor colorFromHexRGB:@"dcdfe1"];
    [cell.contentView addSubview:sepLine];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            [self loadUserScore];
        } ];
        
        return;
    }
    
    switch (indexPath.row) {
        case 0:{
            HRUserGoodsListViewController *viewC = [[HRUserGoodsListViewController alloc] initWithNibName:@"HRUserGoodsListViewController" bundle:nil];
            viewC.orderListType=ORDERLIST_TYPE_ALL;
            [self.tabBarController.navigationController pushViewController:viewC animated:YES];
            break;
        }
            
        case 1:{
            HRMyPreferentViewController *viewC = [[HRMyPreferentViewController alloc] initWithNibName:@"HRMyPreferentViewController" bundle:nil];
            [self.tabBarController.navigationController pushViewController:viewC animated:YES];
            break;
        }
        case 2:{
            HRTakeAddressViewController *viewC = [[HRTakeAddressViewController alloc] initWithNibName:@"HRTakeAddressViewController" bundle:nil];
            [self.tabBarController.navigationController pushViewController:viewC animated:YES];
            break;
        }
        case 3:{
            //打开“我的收藏”
            [self actionToCollect:nil];
            
            break;
        }
        default:
            break;
    }
}


#pragma mark -头像
//- (void)avatarClick
//{
//    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"上传头像"
//                                                             delegate:self
//                                                    cancelButtonTitle:@"取消"
//                                               destructiveButtonTitle:nil
//                                                    otherButtonTitles:@"拍照", @"从手机上获取", nil];
////    actionSheet.tag = kActionSheetTag + 1;
//    [actionSheet showInView:self.view.window];
//}


#pragma mark - 获取积分
-(void)loadUserScore{
    [self showLoadingHud:kLoading];
    [HRInterfaceManager getTotalPointsById:^(BOOL isSucceed, NSString *message, id data) {
        [self hideLoadingHud];
        if (isSucceed) {
            _lblScore.hidden = NO;
            _lblScore.text = [NSString stringWithFormat:@"海尔积分(%@)",[HRCommonData shareCommonData].curUser.userScore ];
        }else{
            [self showTextHUD:message];
        }
            
    }];
}

#pragma mark - 订单
- (IBAction)actionChooseOrder:(id)sender {
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            [self loadUserScore];
        } ];
        
        return;
    }
    
    UIButton *btn = (UIButton*)sender;
    HRUserGoodsListViewController *viewC = [[HRUserGoodsListViewController alloc] initWithNibName:@"HRUserGoodsListViewController" bundle:nil];
    switch (btn.tag-ORDER_BTN_TAG) {
        case 0:
            viewC.orderListType = ORDERLIST_TYPE_WAIT_PAYMENT;
            break;
        case 1:
            viewC.orderListType = ORDERLIST_TYPE_WAIT_SEND;
            break;
        case 2:
            viewC.orderListType = ORDERLIST_TYPE_WAIT_RECEIVE;
            break;
        case 3:
            viewC.orderListType = ORDERLIST_TYPE_RECEIVED;
            break;
        default:
            break;
    }
    [self.tabBarController.navigationController pushViewController:viewC animated:YES];
}

- (IBAction)actionToAfterSale:(id)sender {
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            [self loadUserScore];
        } ];
        
        return;
    }
    
    HROrderApplyListViewController *applyListViewController=[[HROrderApplyListViewController alloc] initWithNibName:NIBNAME_ORDER_REFUND_AND_AFTER_SALE_APPLY_LIST bundle:nil];
    
    [self.tabBarController.navigationController pushViewController:applyListViewController animated:YES];
}

- (IBAction)actionToCollect:(id)sender {
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            [self loadUserScore];
        } ];
        
        return;
    }
    
    HRMyCollectViewController *viewC = [[HRMyCollectViewController alloc] initWithNibName:@"HRMyCollectViewController" bundle:nil];
    [self.tabBarController.navigationController pushViewController:viewC animated:YES];

}

- (IBAction)actionToMessage:(id)sender {
    
    if ([HRCommonData shareCommonData].curUser==nil) {
        [kAppDelegate showUserLoginCompletion:^{
            [self loadUserScore];
        } ];
        
        return;
    }
    
    HRMessageViewController *viewC = [[HRMessageViewController alloc] initWithNibName:@"HRMessageViewController" bundle:nil];
    [self.tabBarController.navigationController pushViewController:viewC animated:YES];
}
@end
